/**
 *@info 选择部门插件
 *@author: HJ
 *@date: 2012-05-24
 * */
if(typeof depchoice == "undefined"){
		if(typeof window.jQuery == "undefined"){
		
	   thorw('由于没有加载jQuery库，所以本功能不能使用，请加载jQuery库，谢谢合作！');
	}
	function depchoice(_objs){
		
		var $sObj = this;
		
		var outStr = '';
		
		var tree_mapp = [];
		var _tree_mapp = [];
		var _search_top = 0;
		var posX;  
		var posY;  
		var popDiv;  
		var dragable;
		var _nums = 0;
		
		var _changeid_arr = [];
		
		var _changename_arr = [];
		
		var _changechkid_arr = [];
		
		var _node_id = 0;
		
		var _node_name = '';
		
		var _replace_arr = [];
		
		$sObj.args = $.extend({
			id:'',/*绑定的位置*/
			treeid:'d_tree',/*树绑定的位置*/
			topname:'',/*树的顶级名称*/
			left:'',/*组件左边位置*/
			top:'',/*组件顶部位置*/
			/*数据源（local为本地生成好的json串格式，ajax为需要动态加载数据的方式，需要配合url和ajaxdata使用,tree_JSON 构建树的内容,search_JSON构建搜索的内容，tree_JSON 和 search_JSON 只有在type为local时使用）*/
			datasource:{"type":"local","url":"","ajaxdata":"","tree_JSON":"","search_JSON":""},
			inputval:'',/*需要显示的数据库，格式为json串*/
			treeobj:'_tree',/*构建树的对象名称*/
			rs:function(){},/*获得结果函数*/
			fun:function(){},/*外部函数*/
			ver:'ver 1.0.0.0'/*版本*/
		},_objs||{});
		
		
		/**
		 *@info 初始化设置
		 *@author: HJ
		 *@date: 2012-05-22
		 *@param null
		 *@return null
		 * */
		$sObj.creates = function(){
			
			/*构建html*/
			$sObj.createhtml();
			
			/*构建树*/
			$sObj.mztree($sObj.args.topname,$sObj.args.treeid,1,$sObj.args.datasource.tree_JSON);
			
			/*层移动*/
			$sObj.divmove();
			
			/*树搜索*/
			$sObj.search();
			
			/*绑定添加和删除按钮事件*/
			$sObj.optevent();
			
			/*搜索框事件*/
			$sObj.autocomplete();
			
			/*关闭、确定和取消按钮的单击事件*/
			$sObj.btnevent();
			
			/*回显数据*/
			$sObj.reshowdata();
			
			/*绑定上移和下移事件*/
			$('#btn_s').click(function(){
				
				$sObj.sort('up');
				
			});
			
			$('#btn_x').click(function(){
				
				$sObj.sort('next');
				
			});
			
		}
		
		/**
		 *@info 初始化数据源
		 *@author: HJ
		 *@date: 2012-05-24
		 *@param null
		 *@return null
		 * */
		$sObj.init = function(){
			
			$sObj.args.inputval = $sObj.inputval;
			
			if ($sObj.args.datasource.type == 'ajax'){
				
				/* 开始保存 */
				$.ajax({
					type:'post',
					url:$sObj.args.datasource.url,
					data:{
						type:'ajax',
						data:JSON.encode($sObj.args.datasource.ajaxdata)
					},
					success:function(r){
						
						var ok = errorHandler(r);
				    
						if(ok){
									$sObj.args.datasource.tree_JSON = ok._arr_tree;
							
								$sObj.args.datasource.search_JSON = ok._arr_search;
								
								$sObj.creates();
							
						}else{
								
							alert("获取数据源失败！");
						}
						}
				});
				
			}
			else
			{
				$sObj.creates();
			}
		}
		
		/**
		 *@info html建立
		 *@author: HJ
		 *@date: 2012-05-24
		 *@param null
		 *@return null
		 * */
		$sObj.createhtml = function(){
			
			if ($sObj.args.left == '' || $sObj.args.top == '')
			{
				$sObj.args.left = (document.body.clientWidth - 512)/2;
		   		$sObj.args.top = (document.body.clientHeight - 400)/2;
			}
			
			$sObj.outStr =  '<div style="position: absolute;left:'+$sObj.args.left+';top:'+$sObj.args.top+';z-index:99999"  id="dep-choice" >';
			$sObj.outStr += '<div style="display:none">';
			$sObj.outStr += '<select size="6" id="select_group_left"></select>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css30" id="select_dep" >';
			$sObj.outStr += '<div class="color1">';
			//$sObj.outStr += '<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>';
			$sObj.outStr += '<div class="css1">';
			$sObj.outStr += '<div class="css2" id="titles">';
			$sObj.outStr += '<div class="css3"><font class="css4">&nbsp;选择部门</font></div>';
			$sObj.outStr += '<div class="css5" ><font class="css6" id="btn_close_winodw">&nbsp;关闭</font></div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css7" id="dep_edit">';
			$sObj.outStr += '<div class="css8">';
			$sObj.outStr += '<div class="css9">';
			$sObj.outStr += '<div class="css10">';
			$sObj.outStr += '<div class="searchs"><input id="input_search" type="text" class="ipt1"/><input id="btn_search" type="button" class="ipt2"/></div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css11">';
			$sObj.outStr += '<div class="css12">';
			$sObj.outStr += '<div class="css13">组织架构</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css14">';
			$sObj.outStr += '<div id="treestyle" class="css15"></div>';
			$sObj.outStr += '<div id="d_tree" class="css16"></div>';
			$sObj.outStr += '<div id="localsite">&nbsp;</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css17">';
			$sObj.outStr += '<div class="css18">';
			$sObj.outStr += '<button id="btn_addgroup"  class="add css19"></button>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css20">';
			$sObj.outStr += '<button id="btn_delgroup" class="del css21"></button>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css22">';
			$sObj.outStr += '<div class="css23">';
			$sObj.outStr += '已选择';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css24">';
			$sObj.outStr += '<div class="select">';
			$sObj.outStr += '<select size="17"  id="select_group_right"></select>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '<div class="css25">';
			$sObj.outStr += '<div class="css26">';
			$sObj.outStr += '<button id="btn_s" style="display:none">上上</button><button id="btn_x" style="display:none">下下</button><button id="btn_confirm" class="save css27"></button>';
			$sObj.outStr += '&nbsp;';
			$sObj.outStr += '<button id="btn_cancel" class="qx css28"></button>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			//$sObj.outStr += '<b class="b5"></b><b class="b6"></b><b class="b7"></b><b class="b8"></b> ';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			$sObj.outStr += '</div>';
			
			$('#'+$sObj.args.id).append($sObj.outStr);
			
			$('#dep-choice').show();
		}
		
		$sObj.display = function(opt,func){
			
			if (opt == 'none'){
				$('#dep-choice').hide();
			}
			
			if (opt == 'show'){
				func();
			}
		}
		
		$sObj.reshowdata = function(){
			$sObj.args.inputval = eval($sObj.args.inputval);
			
			if ($sObj.args.inputval.length > 0)
			{
				var _option = [];
				for (var i = 0;i < $sObj.args.inputval.length; i++)
				{
					_option[i] = $sObj.args.inputval[i].id;
				}
				
			}
			
			if (typeof _option != 'undefined')
			{
				if (_option.length > 0)
				{
					var sitetimes = setTimeout(function(){
						var _arr_selected = [];
						
						for (var ii = 0;ii < _option.length;ii++){
							
							_arr_selected[ii] = _tree_mapp[_option[ii]];
							
						}
						
						var _arr = [[tree_mapp],_arr_selected];
						
						$sObj.mapp(_arr);
						
						$('#select_group_right').html($('#select_group_left').html());
						},500);
				}
			}
			
		}
		
		/**
		 *@info 获得焦点改变树结点颜色
		 *@author: HJ
		 *@date: 2012-05-24
		 *@param string tree_tag 为树生成所绑定的ID
		 *@return null
		 * */
		$sObj.focus = function(tree_tag){
			
			$('.MzTreeview').removeClass('changes');
			$(tree_tag+"_link_"+_tree.getindex()).addClass('changes');
		}
		
		$sObj.clearval = function(){
			
			$('#'+$sObj.args.id).empty();
			
			outStr = '';
			
			tree_mapp = [];
			_tree_mapp = [];
			_search_top = 0;
			posX;  
			posY;  
			popDiv;  
			dragable;
			_nums = 0;
			
			_changeid_arr = [];
			
			_changename_arr = [];
			
			_changechkid_arr = [];
			
		}
		
		$sObj.btnevent = function(){
			
			/*关闭窗体*/
			$('#btn_close_winodw,#btn_cancel').click(function(){
				
				$('#dep-choice').hide();
				$sObj.clearval();
				
				$sObj.args.fun();
				
			});
			
			/*开启窗体*/
			$('#opendiv').click(function(){
				
				$sObj.clearval();
				
				$('#dep-choice').show();
				
			});
			
			/*获得选中值*/
			$('#btn_confirm').click(function(){
				
				var returnVal = [];
				
				$('#select_group_right option').each(function(e){
					
					returnVal[e] = {"id":$(this).val(),"name":$(this).html()};
					
				});
				
				$('#dep-choice').hide();
					$sObj.args.rs(JSON.encode(returnVal));
				
				$sObj.clearval();
			});
			
		}
		$sObj.autocomplete = function(){
			
			$('#input_search').autocomplete($sObj.args.datasource.search_JSON,{
                max: 12,					/*列表里的条目数*/
                minChars: 0,     			/*自动完成激活之前填入的最小字符*/
                scrollHeight: 300,   	/*提示的高度，溢出显示滚动条*/
                matchContains: true,    /*包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示*/
                autoFill: false,    		/*自动填充*/
                formatItem: function(row, i, max){
                    return row.s_name;
                	},
                formatMatch: function(row, i, max){
                    return row.s_name;
                	},
                formatResult: function(row){
                		return row.s_name;
                	}
            }).result(function(event, row, formatted) {
	            	tree_mapp = [];
					for (var i = 0;i < row.s_mapp.length; i++)
					{
						tree_mapp[i] = row.s_mapp[i].Name;
					}
            });
		}
		
		$sObj.optevent = function(){
			
				$('#btn_addgroup,#btn_delgroup').click(function(){
				
				switch ($(this).attr('id'))
				{
					case 'btn_addgroup':
						if ($('#select_group_left').html() != '')
						{
							$('#select_group_right').empty();
							
							$('#select_group_right').append($('#select_group_left').html());
						}
						
						break;
						
					case 'btn_delgroup':
						
						var right_val = $('#select_group_right').val();
						
						var right_html = $('#select_group_right option[value='+right_val+']').html();
						
						var rigth_title = $('#select_group_right option[value='+right_val+']').attr('title');
						
						if ( right_val == null)
						{
							//alert("请选择需要删除项后进行操作！");
							return false;
						}
						
						var right_label = $('#select_group_right option[value='+right_val+']').parent().attr('label');
						
						$('#select_group_left option[value='+right_val+']').remove();
						
						$('#select_group_right option[value='+right_val+']').remove();
						
						var label_html = $('#select_group_right optgroup[label='+right_label+']').html();
						
						if (label_html == '')
						{
							
							$('#select_group_right optgroup[label='+right_label+']').remove();
						}
			
						$sObj.delarr(right_val,right_html);
						
						$('#cbox'+rigth_title).attr('checked',false);
						
						break;
					default:
						break;
				}
			});
		}
		
		$sObj.search = function(){
			
			/*搜索事件*/
			$('#btn_search').click(function(){
				
				var sitetimes = setTimeout(function(){
					
					var _arr_selected = [];
					
					$('#select_group_left option').each(function(e){
						
						_arr_selected[e] = _tree_mapp[$(this).val()];
						});
					
					var _arr = [[tree_mapp],_arr_selected];
					
					$sObj.mapp(_arr);
					
				},500);
				
			});
			
		}
		
		$sObj.divmove = function(){
			
			$('#dep-choice').mousedown(function(e){
				
				$sObj.down(e);
				
			}).mouseup(function(){
				
				$sObj.up();
				
			});
			
			$('#dep_edit').mouseover(function(){
				
				$('#dep-choice').unbind('mousedown');
				$('#dep-choice').unbind('mouseup');
				
			}).mouseout(function(){
				$('#dep-choice').mousedown(function(e){
					
					$sObj.down(e);
					
				}).mouseup(function(){
					
					$sObj.up();
					
				});
				
			});
		}
		
		/**
		 *@info 回显路径
		 *@author: HJ
		 *@date: 2012-05-24
		 *@param array tree_mapp_args 回显路径数组
		 *@return null
		 * */
		$sObj.mapp = function(tree_mapp_args){
			
			for (var j = 0;j < tree_mapp_args.length;j++)
			{
				
				if (typeof tree_mapp_args == 'undefined')
				{
					continue;
				}
				
				for (var i = 0;i < tree_mapp_args[j].length;i++)
				{
				
					if (typeof tree_mapp_args[j][i] == 'undefined')
					{
						continue;
					}
					for (var k = 0;k < tree_mapp_args[j][i].length;k++)
					{
						
						$('.MzTreeview').each(function(){
							
								if ($(this).attr('title') == tree_mapp_args[j][i][k])
								{
									
									
									var _f_id_arr = $(this).attr('id').split('_');
									
									var _f_id = _f_id_arr[_f_id_arr.length-1];
									
									$('#_tree_icon_'+_f_id).attr('src',function(){
										
										return '/assets/misc/vendors/jquery/MzTreeView/imgs/folderopen.gif';
										
									});
								
									if ($('#_tree_display_'+_f_id).css('display') == 'none')
									{
										
										$('#_tree_expand_'+_f_id).trigger('click');								
									}
									
									
									if (j > 0 && k == (tree_mapp_args[j][i].length - 1))
									{
										
										$('#cbox'+_f_id).attr('checked','checked');
										$('#cbox'+_f_id).trigger('click');
										$('#cbox'+_f_id).attr('checked','checked');
									}
									
									
									if (j == 0)
									{
										$('.MzTreeview').removeClass('changes');
										
										$('#'+$(this).attr('id')).addClass('changes');
										
										_search_top = $(this).attr('id');
									}
			
								}
				
						});
					}
				}
			}
			
			/*用于搜索的时候定位滚动条*/
			if (typeof $("#" + _search_top).position() != 'undefined')
			{
				
				$('#'+$sObj.args.treeid).scrollTop(0);
				try
				{
					if ($('#'+_search_top).position().top > $('#localsite').position().top)
					{
						$('#'+$sObj.args.treeid).scrollTop($('#'+_search_top).position().top - $('#'+$sObj.args.treeid).scrollTop());
						
						return false;
					}
				}
				catch(e)
				{
					/*忽略异常*/
				}
			}
		}
		
		
		/**
		 *@info 构建树 
		 *@author: HJ
		 *@date: 2012-03-15
		 *@param string tag_name 根节点名称
		 *@param string tag_tree 树的停靠点
		 *@param string tag_select 选中的节点
		 @param string str_JSON 生成树的内容
		 *@return null
		 * */
		$sObj.mztree = function(tag_name,tag_tree,tag_select,str_JSON){
			
			/*
			if (typeof _tree == 'undefined')
			{
				ebox("构建树失败！请使用 var _tree = '_tree' 定义树对象的全局变量！");
				return false;
			}
			*/
			
			/*树对象*/
			eval(" "+$sObj.args.treeobj+" = '"+$sObj.args.treeobj+"'; ");
			
			
			/*树选中的焦点ID*/
			eval("_focus_id = '"+$sObj.args.treeobj+"';");
			/*构建树对象*/
			eval("var obj = " + eval($sObj.args.treeobj) + " = new MzTreeView(" + eval($sObj.args.treeobj) + ")");
			
			var JD = str_JSON;
			
			/*生成树的内容*/
			if (typeof str_JSON == 'string'){
				
				JD = JSON.decode(str_JSON);
			
			}
			
			if (tag_name == '')
			{
				tag_name = JD.d_company;
			}
			
			/*固定设置树的根目录*/
			var fun_method_all = "method:showdata(0,'"+tag_name+"');";
			/*允许设置和修改节点图片*/
			obj.icons["book"]  = "book.gif";
			
			obj.icons['file'] = "folderopen.gif";
			
			/*树的根节点字体颜色和背景颜色*/
			obj.colors['highLight'] = '';
			obj.colors['highLightText'] = '#000000';
			
			/*展开时对应的图片*/
			obj.iconsExpand["book"] = "bookopen.gif";
		    
			/*兼容树文字标记，原来的是a标记跟现有框架有冲突，故修改为span*/
			obj.compatible_font = 'span';
			
			obj.style_tag = 'treestyle';
			
			/*树的各种图片的路径*/
			obj.setIconPath("/assets/misc/vendors/jquery/MzTreeView/imgs/");
			
			if (typeof tag_name == 'undefined')
			{
				tag_name = '';
			}
			
			/*根节点*/
			obj.nodes["-1_0"] = "text:"+"<span> </span>" + tag_name+";" + fun_method_all;
			
			/*选中的节点*/
			//obj.selected = tag_select;
			
			/*开启树的checkbox*/
			obj.checkbox = true;
			
			/*控制checkbox选择的个数*/
			obj.chknums = 1006;
			
			/*生成树内容*/
			
			if (typeof  JD == 'object')
			{
				for (var i in JD)
				{
					if (typeof JD[i] != 'undefined' && typeof JD[i].d_mapp != 'undefined' && JD[i].d_mapp != '' && JD[i] != '' && JD[i] != false)
					{
						var _mapp_name = [];
						
						for (var ii = 0;ii < JD[i].d_mapp.length; ii++)
						{
							_mapp_name[ii] = JD[i].d_mapp[ii].Name;
						}
						_tree_mapp[i.split('_')[i.split('_').length-1]] = _mapp_name;
					}
					var _tag = JD[i].d_ParentID + "_" + JD[i].d_name_id;
					
					var _name = JD[i].d_name;
					
					var ShowIndex = JD[i].d_ShowIndex;
					
					obj.nodes[_tag] = "depid:"+JD[i].d_name_id+";ShowIndex:"+ShowIndex+";text:"+_name+";title:"+_name+"["+JD[i].d_name_id+"];method:showdata(" + JD[i].d_name_id + "," + JSON.encode(JD[i].d_name) + ")";
					
					/*添加checkbox按钮事件*/
					obj.cbnodes[_tag] = "text:"+_name+";title:"+_tree_mapp+";method:showdatas(" + JD[i].d_name_id + "," + JSON.encode(JD[i].d_name) + ",$chk,$id);";
				}
			}
			
			/*显示树*/
			$('#'+tag_tree).html(obj.toString());
		}
		
		/**
		 *@info 选择组(多选)
		 *@author: HJ
		 *@date: 2012-03-30
		 *@param int id 文件夹ID
		 *@param string name 文件夹名称
		 *@param boolean $chk 返回checkbox是否选中
		 *@param int chockid 树节点id
		 *@return null
		 * */
		showdatas = function(id,name,$chk,chockid){
			
			var _opt_str = '';
			if ($chk == true)
			{
				for (var i =0;i<_changeid_arr.length;i++)
				{
					if (id == _changeid_arr[i])
					{
						return false;
					}
				}
				
				
				_changeid_arr.push(id);
				_changename_arr.push(name);
				_changechkid_arr.push(chockid);
				
				_opt_str = '<option value='+id+' title='+chockid+'>'+name+'</option>';
				
				$('#select_group_left').append(_opt_str);
			}
			if ($chk == false)
			{		
				$sObj.delarr(id,name);
				
				for (var chki in _changechkid_arr)
				{
					if (_changechkid_arr[chki] == chockid)
					{
						_changechkid_arr.splice(chki,1);
					}
				}
				
				$('#select_group_left option[value='+id+']').remove();
			}
		}
		
		/**
		 *@info 选择组
		 *@author: HJ
		 *@date: 2012-03-15
		 *@param int id 文件夹ID
		 *@param string name 文件夹名称
		 *@return null
		 * */
		showdata = function(id,name){
			
			_node_id = id;
			
			_node_name = name;
			
			/*获得焦点改变树结点颜色*/
			$sObj.focus('#'+_focus_id);
		}
		
		$sObj.delarr = function(_val,_name){
			
			for (var i in _changeid_arr)
			{
				if (_changeid_arr[i] == _val)
				{
					_changeid_arr.splice(i,1);
				}
			}
			
			for (var ii in _changename_arr)
			{
				if (_changename_arr[ii] == _name)
				{
					_changename_arr.splice(ii,1);
				}
			}
		}
		
		$sObj.sort = function(act){
			var id = _node_id;
			
			var name = _node_name;
			
			var previd = 0;
			
			var nextid = 0;
			
			var _layer = [];
			
			$('.MzTreeview').each(function(){
				
				if ($(this).attr('title') == name+"["+id+"]")
				{ 
					/*当前选中的节点*/
					var _id_arr =  $(this).attr('id').split("_");
					
					/*当前选中的节点id*/
					var _tree_tree_int ="_tree_tree_" + _id_arr[_id_arr.length-1];
					
					var _tree_display_int = "_tree_display_" + _id_arr[_id_arr.length-1];
					
					var map = $('#'+_tree_display_int).attr('parent_id') + _tree_display_int;
					
					
					
					/*当前选中的节点的内容*/
					var _insert = $('#'+_tree_tree_int).html();
					
					var _pre_str = '<span id="'+_tree_tree_int+'" parent_id = "'+$('#'+_tree_tree_int).attr("parent_id")+'" >';
					switch (act){
					
						case 'up':
							
							var _prev_index = $('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('showindex');
							
							var _dept_prev_id = $('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('depid');
							
								$sObj.upline(_tree_tree_int,_tree_display_int,_layer,_id_arr,previd,_pre_str,_insert);
								
							if (typeof $('#'+_tree_tree_int).next().attr('id') != 'undefined'){
								var _next_id = $('#'+_tree_tree_int).next().attr('id').split("_").reverse()[0];
								
								var _dept_next_id = $('#_tree_link_'+_next_id).attr('depid');
								
								var _next_index = $('#_tree_link_'+_next_id).attr('showindex');
								
								$('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('showindex',_next_index);
								
								$('#_tree_link_'+_next_id).attr('showindex',_prev_index);
								
								_replace_arr.push([_prev_index,_dept_next_id]);
								_replace_arr.push([_next_index,_dept_prev_id]);
							}
							break;
						case 'next':
							
							var _next_index = $('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('showindex');
							
							var _dept_next_id = $('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('depid');
							
							$sObj.downline(_tree_tree_int,_tree_display_int,_layer,_id_arr,nextid,_pre_str,_insert);
							
							if (typeof $('#'+_tree_tree_int).prev().attr('id') != 'undefined'){
								var _prev_id = $('#'+_tree_tree_int).prev().attr('id').split("_").reverse()[0];
								
								var _dept_prev_id = $('#_tree_link_'+_prev_id).attr('depid');
								
								var _prev_index = $('#_tree_link_'+_prev_id).attr('showindex');
								
								$('#_tree_link_'+_id_arr[_id_arr.length-1]).attr('showindex',_prev_index);
								
								$('#_tree_link_'+_prev_id).attr('showindex',_next_index);
								
									_replace_arr.push([_prev_index,_dept_next_id]);
								_replace_arr.push([_next_index,_dept_prev_id]);
							}
							
							
							break;
						default:
							break;
					}
					
					/* 最后节点的id*/
					
					var _last_node_id = $('#'+_tree_tree_int).parent().children().last().attr('id');
					var _last_node_id_arr  = _last_node_id.split('_');
					var _last_node_id_int = _last_node_id_arr[_last_node_id_arr.length - 1];
					
					/*最后节点的上一级节点id*/
					var _last_up_node_id = $('#_tree_tree_'+_last_node_id_int).prev().attr('id')
					
					if (typeof _last_up_node_id == 'undefined')
					{
						return false;
					}
					
					var _last_up_node_id_arr  = _last_up_node_id.split('_');
					var _last_up_node_id_int = _last_up_node_id_arr[_last_up_node_id_arr.length - 1];
					
					/*上一级节点id的下一个节点的id*/
					var _last_next_node_id = $('#_tree_tree_'+_last_up_node_id_int).next().attr('id')
					var _last_next_node_id_arr  = _last_next_node_id.split('_');
					var _last_next_node_id_int = _last_next_node_id_arr[_last_next_node_id_arr.length - 1];
					
					if (typeof _tree.treenode(_last_node_id_int).childNodes != 'undefined'){
						
						var _change_id = _last_node_id_int;
						$('#_tree_expand_'+_change_id).trigger('click');
						$('#_tree_expand_'+_change_id).trigger('click');
						
						if (_id_arr[_id_arr.length-1] != _last_next_node_id_int){
							_change_id = _last_next_node_id_int;
						}
						
						$sObj.treenode(_change_id);
						
						$('#_tree_display_'+_change_id+" span").each(function(){
							
							var _replace_str = '';
							
							if (typeof $(this).attr('id') != 'undefined' && $('#'+$(this).attr('id')+' div nobr span').html() != null)
							{
								
								var _layer_id = $(this).attr('parent_id');
								var _layer_arr = _layer_id.split(" ");
								
								delete(_layer_arr[0]);
								delete(_layer_arr[1]);
								
								_layer_arr.sort();
								
								for (var i = 0;i < _layer_arr.length; i++){
									
									if (parseInt(_layer_arr[i]) > 0){
										
										_layer[_layer_arr[i]] = {layer:(i+1),line:false}
									}
								}
								
								
								var _sub_id_arr = $(this).attr('id').split("_");
								var _sub_id = _sub_id_arr[_sub_id_arr.length - 1];
				
									
								var $create_next = '';
								
								var _layer_id = _change_id;
								
								
								if ($('#fspan' + _sub_id).html().indexOf('L4.gif'))
								{
								
									for (var iii = 0;iii < _layer[_layer_id].layer-1; iii++)
									{
										
											$create_next += ".next()";
										
									}
									_replace_str = eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src')");
									
									_replace_str = _replace_str.replace('L4.gif','L5.gif');
									
									
									eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src','"+_replace_str+"')");
									
									
								}
								
								
							}
						});
						
						if (act == 'next')
						{
							$sObj.createupline(_id_arr,_last_node_id_int,_last_up_node_id_int,_layer);
						}		
					}
					else
					{
						
						$sObj.createupline(_id_arr,_last_node_id_int,_last_up_node_id_int,_layer);
						}
					if (_last_node_id == _tree_tree_int || _last_up_node_id == _tree_tree_int){
						$('#_tree_expand_'+_last_up_node_id_int).attr('src',function(){
						
							var _src_split_split = $(this).attr('src').split("/");
							
							var _src_split_gif = _src_split_split[_src_split_split.length - 1];
							
							if (_src_split_gif == 'P2.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/P1.gif';
							}
							
							if (_src_split_gif == 'M2.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/M1.gif';
							}
							
							if (_src_split_gif == 'L2.gif' )
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/L1.gif';
							}
						});
						
						$('#_tree_expand_'+_last_node_id_int).attr('src',function(){
					
							var _src_split_split = $(this).attr('src').split("/");
							
							var _src_split_gif = _src_split_split[_src_split_split.length - 1];
							
							
							if (_src_split_gif == 'P2.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/M2.gif';
							}
							
							if (_src_split_gif == 'M1.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/M2.gif';
							}
							
							if (_src_split_gif == 'P1.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/P2.gif';
							}
							
							if (_src_split_gif == 'L1.gif')
							{
								return '/assets/misc/vendors/jquery/MzTreeView/imgs/L2.gif';
								
								if (_src_split_gif == 'L2.gif')
								{
									return '/assets/misc/vendors/jquery/MzTreeView/imgs/L1.gif';
								}
							}
						});
					}
					
					return false;
				}
				
			});
			
		}
		
		$sObj.createupline = function(_id_arr,_last_node_id_int,_last_up_node_id_int,_layer){
			var _change_id = _last_node_id_int;
			
			if (_id_arr[_id_arr.length-1] != _last_up_node_id_int){
				_change_id = _last_up_node_id_int;
			}
			else
			{
				return false;
			}
			
			$('#_tree_expand_'+_change_id).trigger('click');
			
			$('#_tree_expand_'+_change_id).trigger('click');
			
			$sObj.treenode(_change_id);
			$('#_tree_display_'+_change_id+" span").each(function(){
				
				var _replace_str = '';
				
				if (typeof $(this).attr('id') != 'undefined' && $('#'+$(this).attr('id')+' div nobr span').html() != null)
				{
					
					var _layer_id = $(this).attr('parent_id');
					var _layer_arr = _layer_id.split(" ");
					
					delete(_layer_arr[0]);
					delete(_layer_arr[1]);
					
					_layer_arr.sort();
					
					for (var i = 0;i < _layer_arr.length; i++){
						
						if (parseInt(_layer_arr[i]) > 0){
							
							_layer[_layer_arr[i]] = {layer:(i+1),line:true}
						}
					}
					
					var _sub_id_arr = $(this).attr('id').split("_");
					var _sub_id = _sub_id_arr[_sub_id_arr.length - 1];
					
					var $create_next = '';
					
					var _layer_id = _change_id;
					
					
					if ($('#fspan' + _sub_id).html().indexOf('L5.gif'))
					{
						
						for (var iii = 0;iii < _layer[_layer_id].layer-1; iii++)
						{
							
								$create_next += ".next()";
							
						}
						_replace_str = eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src')");
						
						_replace_str = _replace_str.replace('L5.gif','L4.gif');
						
						
						eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src','"+_replace_str+"')");
						
						
					}
					
				}
			});
			
		}
		
		$sObj.upline = function(_tree_tree_int,_tree_display_int,_layer,_id_arr,previd,_pre_str,_insert)
		{
			previd = $('#'+_tree_tree_int).prev().attr('id');
			
			if (typeof previd == 'undefined'){
				
				return false;
				
			}
			
			
			$('#'+_tree_tree_int).remove();
			$('#'+previd).before(_pre_str + _insert + "</span>");
			$('#_tree_expand_'+_id_arr[_id_arr.length-1]).trigger('click');
			
			$('#_tree_expand_'+_id_arr[_id_arr.length-1]).trigger('click');
			
			$sObj.treenode(_id_arr[_id_arr.length-1]);
			
			$('#'+_tree_display_int+" span").each(function(){
				
				var _replace_str = '';
				
				if (typeof $(this).attr('id') != 'undefined' && $('#'+$(this).attr('id')+' div nobr span').html() != null)
				{
					
					var _layer_id = $(this).attr('parent_id');
					var _layer_arr = _layer_id.split(" ");
					
					delete(_layer_arr[0]);
					delete(_layer_arr[1]);
					
					_layer_arr.sort();
					
					for (var i = 0;i < _layer_arr.length; i++){
						
						if (parseInt(_layer_arr[i]) > 0){
							
							_layer[_layer_arr[i]] = {layer:(i+1),line:true}
						}
					}
					
					var _sub_id_arr = $(this).attr('id').split("_");
					var _sub_id = _sub_id_arr[_sub_id_arr.length - 1];
					
					var $create_next = '';
					
					if ($('#fspan' + _sub_id).html().indexOf('L5.gif'))
					{
					
						for (var iii = 0;iii < _layer[_id_arr[_id_arr.length-1]].layer-1; iii++)
						{
							
								$create_next += ".next()";
							
						}
						_replace_str = eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src')");
						
						_replace_str = _replace_str.replace('L5.gif','L4.gif');
						
						
						eval("$('#fspan"+ _sub_id+"').children().first()"+$create_next+".attr('src','"+_replace_str+"')");
						
						
					}
					
				}
			});
		}
		
		$sObj.downline = function(_tree_tree_int,_tree_display_int,_layer,_id_arr,nextid,_pre_str,_insert)
		{
			nextid = $('#'+_tree_tree_int).next().attr('id');
			
			if (typeof nextid == 'undefined'){
				return false;
			}
			
			$('#'+_tree_tree_int).remove();
			
			$('#'+nextid).after(_pre_str + _insert + "</span>");
			
			$('#_tree_expand_'+_id_arr[_id_arr.length-1]).trigger('click');
			
			$('#_tree_expand_'+_id_arr[_id_arr.length-1]).trigger('click');
			
			$sObj.treenode(_id_arr[_id_arr.length-1]);
		}
		
		$sObj.treenode = function(id){
		
			var _dq_ = _tree.treenode(id).childNodes;
			
			$sObj.dg(_dq_);
		}
		
		$sObj.dg = function(_obj){ 
			
			if (typeof _obj == 'undefined')
			{
				return false;
			}
			
			for (var i = 0;i < _obj.length;i++){
				
				if (_obj[i].hasChild == true)
				{
					$('#_tree_expand_'+_obj[i].id).trigger('click');
					$('#_tree_expand_'+_obj[i].id).trigger('click');
		
					$sObj.dg(_obj[i].childNodes);
				}
				
			}
			
		}
		
		$sObj.nodeup = function(){
			
			$sObj.sort('up');
		}
		
		$sObj.nodedown = function(){
		
			$sObj.sort('next');
			
		}
		
		$sObj.changes = function(){
			
			return _replace_arr;
			
		}
		
		$sObj.down = function(e){ 
			
		    popDiv = document.getElementById("dep-choice");  
		    e = e || window.event; //如果是IE   
		    posX = e.clientX - parseInt(popDiv.style.left);     
		    posY = e.clientY - parseInt(popDiv.style.top);  
		    dragable = true;  
		    document.onmousemove = $sObj.move;  
		}  
		
		$sObj.move = function(ev){  
		    if(dragable == true){  
		        ev = ev || window.event;//如果是IE  
		        popDiv.style.left = (ev.clientX - posX) + "px";     
		        popDiv.style.top = (ev.clientY - posY) + "px";     
		    }  
		}  
		 
		$sObj.up = function(){  
		    dragable = false;  
		} 
		
		$sObj.up = function(){  
		    dragable = false;  
		}
		
		$sObj.inputval = '';
	}
}